<?php /* УТФ-8:fix */

class SPIcontroller__songnvideo__front extends SPIcontroller__songnvideo
{
    public function get_tophits_songs( $limit )
    {
        $r = array();

        $qres = SPIm::I('songnvideo')->front->select( 'song', $limit, 'tophits' );

        while( $item = $qres->fetch() )
        {
            $item['preview'] = SPIm::I('media')->get_public_sourceByIndexNewJpeg(
                $this->MEDIA_PHOTO_GID, $item['id'], 1,
                array( 'mode' => 'preview' )
            );

            $item['sound'] = SPIm::I('media')->get_public_sourceByIndexOld(
                $this->MEDIA_AUDIO_GID, $item['id'], 1
            );

            $r[] = $item;
        }
        $qres->close();

        return $r;
    }
	
	public function get_novelty_songs( $limit )
    {
        $r = array();

        $qres = SPIm::I('songnvideo')->front->select( 'song', $limit, 'novelty' );

        while( $item = $qres->fetch() )
        {
            $item['preview'] = SPIm::I('media')->get_public_sourceByIndexNewJpeg(
                $this->MEDIA_PHOTO_GID, $item['id'], 1,
                array( 'mode' => 'preview' )
            );

            $item['sound'] = SPIm::I('media')->get_public_sourceByIndexOld(
                $this->MEDIA_AUDIO_GID, $item['id'], 1
            );

            $r[] = $item;
        }
        $qres->close();

        return $r;
    }

    public function get_videos( $limit = null )
    {
        $r = array();

        $qres = SPIm::I('songnvideo')->front->select( 'video', $limit );

        $index=0;
        while( $item = $qres->fetch() )
        {
            $item['preview'] = SPIm::I('media')->get_public_sourceByIndexNewJpeg(
                $this->MEDIA_PHOTO_GID, $item['id'], 1,
                array( 'mode' => 'preview' )
            );

            if( $item['anons'] )
            {
                if( preg_match( '/v=([A-Za-z0-9-]+)/', $item['anons'], $matches ) > 0 )
                {
                    $item['youtube'] = $matches[1];
                }
            }
            else
            {
                $item['video'] = SPIm::I('media')->get_public_sourceByIndexOld(
                    $this->MEDIA_VIDEO_GID, $item['id'], 1
                );
            }

            $r[ floor( ($index++)/4) ][] = $item;
        }
        $qres->close();

        return $r;
    }

    public function exec_tophits( & $content )
    {
        $r = array();

        $content['active_topic'] = $content['env_topic'];
        $content['topic_template_filename'] = 'tophits/list.tpl';

        $r['items'] = $this->get_tophits_songs( 100 );

        $content['tophits'] = $r;
        return true;
    }
	
	public function exec_novelty( & $content )
    {
        $r = array();

        $content['active_topic'] = $content['env_topic'];
        $content['topic_template_filename'] = 'novelty/list.tpl';

        $r['items'] = $this->get_novelty_songs( 100 );

        $content['novelty'] = $r;
        return true;
    }
}
